<template>
  <div style="padding: 6px;">
    <el-form ref="searchForm" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="14">
          <el-form-item label="用户姓名">
            <el-input v-model="sd.searchName" size="small" placeholder="用户姓名"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button class="mt_5" type="primary" size="mini" icon="el-icon-search" @click="searchUser">查询</el-button>
          <el-button class="mt_5" type="primary" size="mini" icon="el-icon-plus" @click="addUser">添加</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-table :data="userObj.list" border style="width: 100%">
      <el-table-column label="用户名" prop="username"></el-table-column>
      <el-table-column label="用户姓名" prop="name"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button size="mini" type="primary" @click="editUser(scope.row.id)">修改</el-button>
          <el-button size="mini" type="primary" @click="setUserRole(scope.row.id)">角色设定</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="center" background layout="total, prev, pager, next"
                   @current-change="loadUser" :total="userObj.total"
                   :current-page.sync="sd.pageNum" :page-size="sd.pageSize">
    </el-pagination>

    <el-dialog :title="popupTitle" :visible.sync="entityDetailVisible" width="800px">
      <userDetail v-if="entityDetailVisible" :visible.sync="entityDetailVisible" :entityId="popupEntityId"></userDetail>
    </el-dialog>
    <el-dialog :title="popupTitle" :visible.sync="userRoleVisible" width="880px">
      <userRole v-if="userRoleVisible" :visible.sync="userRoleVisible" :entityId="popupEntityId"></userRole>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import userDetail from '@/views/sys/user/userDetail'
import userRole from '@/views/sys/user/UserRole'
export default {
  components: {userDetail, userRole},
  data() {
    return {
      sd: {
        searchName : '',
        pageNum: 1,
        pageSize: 5
      },
      userObj: {},

      entityDetailVisible : false,
      popupTitle : '',
      popupEntityId : 0,

      userRoleVisible : false,
    }
  },
  mounted() {
    this.loadUser()
    this.$EventBus.$on("useradd", () => {
      this.sd.pageNum = 1;
      this.loadUser();
    });
    this.$EventBus.$on("useredit", () => {
      this.loadUser();
    });
  },
  beforeDestroy() {
    this.$EventBus.$off("useradd")
    this.$EventBus.$off("useredit")
  },
  methods: {
    searchUser(){
      this.sd.pageNum = 1;
      this.loadUser();
    },
    loadUser() {
      request({
        url: '/sys/useradmin/pageuser',
        data: this.sd
      }).then(res => {
        const { data } = res
        this.userObj = data
      })
    },
    addUser(){
      this.popupEntityId = 0;
      this.popupTitle= "新增用户"
      this.entityDetailVisible= true
    },
    editUser(id){
      this.popupEntityId= id;
      this.popupTitle= "修改用户"
      this.entityDetailVisible= true
    },
    setUserRole(id){
      this.popupEntityId= id;
      this.popupTitle= "用户角色"
      this.userRoleVisible= true
    },
  }
}
</script>
